home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ddj0897.zip
/
uthash.asc
< prev
next >
Wrap
Text File
|
1997-06-20
|
3KB
|
104 lines
_Designing C++ Interfaces to C-Language Libraries_
by Larry E. Baker, Jr.
Listing One
typedef struct _HT_LIST { // list of pointers
struct _HT_LIST *next;
void *item;
} HT_LIST;
typedef struct _HASH { // array of lists, hash/compare functions
int size;
HT_LIST **buckets;
int (*hash)(void *);
int (*compare)(void *, void *);
} HASHTABLE;
Listing Two
HASHTABLE *
htcreate( /* create a new hash table */
int size, /* # of buckets in table */
int (*hash_function)(void*), /* user hash function */
int (*search_function)(void*,void*) /* user search function */
);
int
htdestroy( /* destroy hash table */
HASHTABLE * hash_table, /* existing hash table */
int flags /* HT_FREE_REC|0 */
);
int
htinsert( /* insert a new entry */
HASHTABLE * hash_table, /* existing hash table */
void * record /* record ptr to insert */
);
int
htdeleterec( /* delete a table entry */
HASHTABLE * hash_table, /* existing hash table */
void * record, /* record or template */
int flags /* HT_FREE_REC|0 */
);
int
htsearch( /* search and/or remove */
HASHTABLE * hash_table, /* existing hash table */
void * templ, /* search template */
void ** record, /* &ptr for record found */
int flags /* HT_FREE_REC|HT_REPLACE|HT_DELETE|0 */
);
void
htstats( /* print out hash tbl stats */
HASHTABLE * hash_table /* existing hash table */
);
/* for htwalk, note that the function takes two arguments. The */
/* first is a pointer to the object found in the hash table; the */
/* second is an argument passed in the void * arg pointer. */
/* this is done so an argument can be passed "through" the ht walk */
/* function to the user-supplied "func" function. */
void
htwalk( /* walk through the table */
HASHTABLE * hash_table, /* existing hash table */
void (*func)(void *, void*), /* function to apply */
void * arg /* user argument to function */
);
Listing Three
template <class T> class HTable
{
public:
HTable(
int size,
int destroyFlags,
int (*hashFunction)(T*),
int (*compareFunction)(T*, T*)
);
HTable(HASHTABLE * ht);
~HTable();
int insert(T& item);
int deleterec(T& itemTemplate, int flags);
int search(T& itemTemplate, T ** foundItem, int flags);
void stats();
void walk(void (*walkFunction)(T&, void*), void * walkArg);
int rc;
protected:
HASHTABLE * ht;
int dFlags;
static void HTable::walkDestructor( T * pT, void * arg);
};